package com.ruoyi.web.controller.wisdom;

import java.util.List;

import com.ruoyi.common.config.HeadNum;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.framework.util.GetSessionValueUtil;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.wisdom.domain.FzServiceNotice;
import com.ruoyi.wisdom.service.IFzServiceNoticeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.servlet.http.HttpSession;

/**
 * 发布公告Controller
 * 
 * @author ruoyi
 * @date 2020-03-05
 */
@Controller
@RequestMapping("/wisdom/notice")
public class FzServiceNoticeController extends BaseController
{
    private String prefix = "wisdom/notice";

    @Autowired
    private IFzServiceNoticeService fzServiceNoticeService;

    @RequiresPermissions("wisdom:notice:view")
    @GetMapping()
    public String notice()
    {
        return prefix + "/notice";
    }

    /**
     * 查询发布公告列表
     */
    @RequiresPermissions("wisdom:notice:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(HttpSession session,FzServiceNotice fzServiceNotice)
    {
        startPage();
        fzServiceNotice.setcId(GetSessionValueUtil.getCommunity());
        List<FzServiceNotice> list = fzServiceNoticeService.selectFzServiceNoticeList(fzServiceNotice);
        return getDataTable(list);
    }

    /**
     * 导出发布公告列表
     */
    @RequiresPermissions("wisdom:notice:export")
    @Log(title = "发布公告", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(FzServiceNotice fzServiceNotice)
    {
        List<FzServiceNotice> list = fzServiceNoticeService.selectFzServiceNoticeList(fzServiceNotice);
        ExcelUtil<FzServiceNotice> util = new ExcelUtil<FzServiceNotice>(FzServiceNotice.class);
        return util.exportExcel(list, "notice");
    }

    /**
     * 新增发布公告
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存发布公告
     */
    @RequiresPermissions("wisdom:notice:add")
    @Log(title = "发布公告", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(FzServiceNotice fzServiceNotice)
    {
        fzServiceNotice.setNoticeId(HeadNum.NOTICE_HEAD+ DateUtils.getTimeMillis());
        fzServiceNotice.setcId(GetSessionValueUtil.getCommunity());
        fzServiceNotice.setCreateName(ShiroUtils.getSysUser().getUserName());
        return toAjax(fzServiceNoticeService.insertFzServiceNotice(fzServiceNotice));
    }

    /**
     * 修改发布公告
     */
    @GetMapping("/edit/{noticeId}")
    public String edit(@PathVariable("noticeId") String noticeId, ModelMap mmap)
    {
        FzServiceNotice fzServiceNotice = fzServiceNoticeService.selectFzServiceNoticeById(noticeId);
        mmap.put("fzServiceNotice", fzServiceNotice);
        return prefix + "/edit";
    }

    /**
     * 修改保存发布公告
     */
    @RequiresPermissions("wisdom:notice:edit")
    @Log(title = "发布公告", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(FzServiceNotice fzServiceNotice)
    {
        SysUser user = ShiroUtils.getSysUser();
        fzServiceNotice.setUpdateName(user.getUserName());
        return toAjax(fzServiceNoticeService.updateFzServiceNotice(fzServiceNotice));
    }

    /**
     * 删除发布公告
     */
    @RequiresPermissions("wisdom:notice:remove")
    @Log(title = "发布公告", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(fzServiceNoticeService.deleteFzServiceNoticeByIds(ids));
    }
}
